/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wkq.util.span

import android.os.Parcel
import android.text.ParcelableSpan
import android.text.TextPaint
import android.text.style.MetricAffectingSpan
import androidx.annotation.ColorInt


class SizeColorSpan : MetricAffectingSpan, ParcelableSpan {
    val size: Int
    val dip: Boolean
    private val mColor: Int

   /**
   *
    */
    constructor(size: Int, @ColorInt color: Int) : this(size, false, color)

   /**
   *dip 是否DIP展示 false 表示 px展示
    */
    constructor(size: Int, dip: Boolean, @ColorInt color: Int) {
        this.size = size
        this.dip = dip
        mColor = color
    }

    /**
     * Creates an [SizeColorSpan] from a parcel.
     */
    constructor(src: Parcel) {
        this.size = src.readInt()
        this.dip = src.readInt() != 0
        mColor = src.readInt()
    }

    override fun getSpanTypeId(): Int {
        return this.spanTypeIdInternal
    }


    val spanTypeIdInternal: Int
        get() = 16

    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        writeToParcelInternal(dest, flags)
    }


    fun writeToParcelInternal(dest: Parcel, flags: Int) {
        dest.writeInt(this.size)
        dest.writeInt(mColor)
        dest.writeInt(if (this.dip) 1 else 0)
    }


    override fun updateDrawState(ds: TextPaint) {
        if (this.dip) {
            ds.setTextSize(this.size * ds.density)
            ds.setColor(mColor)
        } else {
            ds.setTextSize(size.toFloat())
            ds.setColor(mColor)
        }
    }

    override fun updateMeasureState(ds: TextPaint) {
        if (this.dip) {
            ds.setTextSize(this.size * ds.density)
            ds.setColor(mColor)
        } else {
            ds.setTextSize(size.toFloat())
            ds.setColor(mColor)
        }
    }


    override fun toString(): String {
        return "AbsoluteSizeSpan{size=" + this.size + ", isDip=" + this.dip + '}'
    }
}
